package com.ruoyi.mes.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.mes.domain.*;
import com.ruoyi.mes.mapper.*;
import com.ruoyi.mes.utils.CodeUtils;
import com.ruoyi.mes.utils.PageInfoUtils;
import com.ruoyi.mes.vo.buy.BuyOrderVo;
import com.ruoyi.mes.vo.buy.BuyNeedVo;
import com.ruoyi.mes.vo.list.BuyNeedListVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mes.service.IBuyNeedService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 采购需求Service业务层处理
 *
 * @author LongAndHao
 * @date 2023-04-04
 */
@Service
public class BuyNeedServiceImpl implements IBuyNeedService {
    @Autowired
    private MtrInfoMapper mtrInfoMapper;

    @Autowired
    private ProgPlanMapper progPlanMapper;

    @Autowired
    private ProInfoMapper proInfoMapper;

    @Autowired
    private BuyNeedMapper buyNeedMapper;

    @Autowired
    private BuyOrderMapper buyOrderMapper;

    /**
     * 查询采购需求
     *
     * @param needId 采购需求主键
     * @return 采购需求
     */
    @Override
    public BuyNeed selectBuyNeedByNeedId(Long needId) {
        return buyNeedMapper.selectBuyNeedByNeedId(needId);
    }

    /**
     * 查询采购需求列表
     *
     * @param buyNeed 采购需求
     * @return 采购需求
     */
    @Override
    public List<BuyNeed> selectBuyNeedList(BuyNeed buyNeed) {
        return buyNeedMapper.selectBuyNeedList(buyNeed);
    }

    /**
     * 新增采购需求
     *
     * @param buyNeed 采购需求
     * @return 结果
     */
    @Override
    public int insertBuyNeed(BuyNeed buyNeed) {
        buyNeed.setCreateTime(DateUtils.getNowDate());
        return buyNeedMapper.insertBuyNeed(buyNeed);
    }

    /**
     * 修改采购需求
     *
     * @param buyNeed 采购需求
     * @return 结果
     */
    @Override
    public int updateBuyNeed(BuyNeed buyNeed) {
        buyNeed.setUpdateTime(DateUtils.getNowDate());
        return buyNeedMapper.updateBuyNeed(buyNeed);
    }

    /**
     * 批量删除采购需求
     *
     * @param needIds 需要删除的采购需求主键
     * @return 结果
     */
    @Override
    public int deleteBuyNeedByNeedIds(Long[] needIds) {
        return buyNeedMapper.deleteBuyNeedByNeedIds(needIds);
    }

    /**
     * 删除采购需求信息
     *
     * @param needId 采购需求主键
     * @return 结果
     */
    @Override
    public int deleteBuyNeedByNeedId(Long needId) {
        return buyNeedMapper.deleteBuyNeedByNeedId(needId);
    }

    /**
     * 查 - 采购需求分页 - 采购需求单号
     *
     * @param buyNeed
     * @return
     */
    @Override
    public List<BuyNeedListVo> buyNeedList(BuyNeed buyNeed) {
        List<BuyNeedListVo> buyNeedListVos = new ArrayList<>();

        List<BuyNeed> buyNeeds = buyNeedMapper.buyNeedList(buyNeed);
        if (buyNeeds.size() != 0) {
            for (BuyNeed need : buyNeeds) {
                BuyNeedListVo buyNeedListVo = new BuyNeedListVo();
                buyNeedListVo.setBuyNeed(need);
                //查询物料信息
                List<BuyNeed> buyNeeds1 = buyNeedMapper.selectBuyNeedByNeedCode(need.getNeedCode());
                List<MtrInfo> mtrInfos = new ArrayList<>();
                BigDecimal totalCost = new BigDecimal("0.0");
                for (BuyNeed buyNeed1 : buyNeeds1) {
                    MtrInfo mtrInfo = mtrInfoMapper.selectMtrInfoByCode(buyNeed1.getMtrCode());
                    BigDecimal result = null;
                    result = BigDecimal.valueOf(buyNeed1.getNeedQuantity()).multiply(mtrInfo.getCost());
                    mtrInfos.add(mtrInfo);
                    totalCost = totalCost.add(result);
                }
                buyNeedListVo.setTotalCost(totalCost);
                buyNeedListVo.setMtrInfos(mtrInfos);

                buyNeedListVos.add(buyNeedListVo);
            }
        }

        return PageInfoUtils.toPageInfo(buyNeeds, buyNeedListVos);
    }

    /**
     * 查 - 采购需求详情
     *
     * @param id 采购需求id
     * @return
     */
    @Override
    public BuyNeedVo getDetais(Long id) {
        BuyNeedVo buyNeedVo = new BuyNeedVo();

        BuyNeed buyNeed = buyNeedMapper.selectBuyNeedByNeedId(id);
        buyNeedVo.setNeedCode(buyNeed.getNeedCode());

        BuyNeed need = new BuyNeed();
        need.setNeedCode(buyNeed.getNeedCode());
        List<BuyNeed> buyNeeds = buyNeedMapper.selectBuyNeedList(need);

        List<MtrInfo> mtrs = new ArrayList<>();
        BigDecimal totalCost = new BigDecimal("0.0");
        for (BuyNeed buyNeed1 : buyNeeds) {
            MtrInfo mtrInfo = new MtrInfo();
            mtrInfo.setMtrCode(buyNeed1.getMtrCode());
            mtrInfo.setMtrName(buyNeed1.getMtrName());
            mtrInfo.setMtrQuantity(buyNeed1.getNeedQuantity());
            //计算预算成本
            MtrInfo mtr = mtrInfoMapper.selectMtrInfoByCode(buyNeed1.getMtrCode());
            BigDecimal BudgetedCost = null;
            BudgetedCost = BigDecimal.valueOf(buyNeed1.getNeedQuantity()).multiply(mtr.getCost());
            mtrInfo.setBudgetedCost(BudgetedCost);
            totalCost = totalCost.add(BudgetedCost);

            //查询产品信息
            ProgPlan progPlan = progPlanMapper.selectProgPlanById(buyNeed1.getProgId());
            ProInfo proInfo = proInfoMapper.selectProInfoById(progPlan.getProId());
            buyNeedVo.setProInfo(proInfo);
            mtrs.add(mtrInfo);
        }

        buyNeedVo.setTotalCost(totalCost);
        buyNeedVo.setMtrs(mtrs);
        return buyNeedVo;
    }

    /**
     * 删 - 采购需求
     *
     * @param id
     * @return
     */
    @Override
    public AjaxResult deleteBuyNeed(Long id) {
        BuyNeed need = buyNeedMapper.selectBuyNeedByNeedId(id);

        BuyNeed buyNeed = new BuyNeed();
        buyNeed.setNeedCode(need.getNeedCode());
        List<BuyNeed> buyNeeds = buyNeedMapper.selectBuyNeedList(buyNeed);
        for (BuyNeed buyNeed1 : buyNeeds) {
            buyNeedMapper.deleteBuyNeedByNeedId(buyNeed1.getNeedId());
        }
        return AjaxResult.success("删除成功!");
    }

    /**
     * 生成采购订单
     *
     * @param buyOrderVo
     * @return
     */
    @Override
    @Transactional
    public AjaxResult createBuyorder(BuyOrderVo buyOrderVo) {
        BuyNeed need = buyNeedMapper.selectBuyNeedByNeedId(buyOrderVo.getNeedId());

        //判断采购需求状态，防止重复生成采购订单
        if (need.getStatus() == 1) {
            return AjaxResult.error("已生成采购订单!");
        }

        BuyNeed buyNeed = new BuyNeed();
        buyNeed.setNeedCode(need.getNeedCode());
        //获取采购需求编码相同的数据
        List<BuyNeed> buyNeeds = buyNeedMapper.selectBuyNeedList(buyNeed);
        //生成采购订单编号
        String buyOrderCode = CodeUtils.BuyOrderCodeCreat();
        //查找产品名称
        ProgPlan progPlan = progPlanMapper.selectProgPlanById(need.getProgId());
        ProInfo proInfo = proInfoMapper.selectProInfoById(progPlan.getProId());
        String proName = proInfo.getProName();
        for (BuyNeed buyNeed1 : buyNeeds) {
            BuyOrder buyOrder = new BuyOrder();
            buyOrder.setNeedCode(buyNeed1.getNeedCode());
            buyOrder.setOrderName("测试订单");
            buyOrder.setMtrCode(buyNeed1.getMtrCode());
            buyOrder.setMtrName(buyNeed1.getMtrName());
            buyOrder.setBuyQuantity(buyNeed1.getNeedQuantity());
            buyOrder.setStatus(0L);//默认未采购
            buyOrder.setBuyOrderCode(buyOrderCode);
            buyOrder.setSupplier(buyOrderVo.getSupplier());
            buyOrder.setRemark("测试数据");
            buyOrderMapper.insertBuyOrder(buyOrder);
            //更新采购需求状态为已采购
            buyNeed1.setStatus(1L);
            buyNeedMapper.updateBuyNeed(buyNeed1);
        }
        return AjaxResult.success("采购订单生成成功!");
    }


}
